package com.st.lotte.view;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.st.lotte.dao.TLO_DELY_TRCE_RECV_IF_DAO;
import com.st.lotte.model.TLO_DELY_TRCE_RECV_IF;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
 
public class SimpleExcellView {
	public static void main(String[] args) {
		SimpleExcellView excell = new SimpleExcellView();
      TLO_DELY_TRCE_RECV_IF_DAO dao = new TLO_DELY_TRCE_RECV_IF_DAO();
      Vector<TLO_DELY_TRCE_RECV_IF> list = dao.selectALLRecvData(" limit 100 ", "excell");
		excell.excellFileMake(list, "롯데홈쇼핑_.xls");
	}
	
	public void excellFileMake(Vector<TLO_DELY_TRCE_RECV_IF> data, String fname){
		// 엑셀파일 객체 생성
        WritableWorkbook workbook = null;
        // 시트 객체 생성
        WritableSheet sheet = null;
        // 셀 객체 생성
        Label label = null;
        // 저장할 파일 객체 생성
        File file = new File(fname);
        
        // 검색된 데이터 가져오기
//        TLO_DELY_TRCE_RECV_IF_DAO dao = new TLO_DELY_TRCE_RECV_IF_DAO();
//        Vector<TLO_DELY_TRCE_RECV_IF> list = dao.selectALLRecvData(" limit 100 ", "excell");
        Vector<TLO_DELY_TRCE_RECV_IF> list = data;
         
        // excell 형태 데이터 만들기
        Vector<Map<String,Object>> excellDataList = new Vector<Map<String,Object>>();
        System.out.println("list=="+list);
        for (int i = 0; i < list.size(); i++) {
        	HashMap hm_1 = new HashMap() ;
        	TLO_DELY_TRCE_RECV_IF dto = list.get(i);
        	hm_1.put("수신일자(롯데기준)",dto.getRECV_DATE().toString()) ;
        	hm_1.put("운송장번호", dto.getSLIP_NO());
        	hm_1.put("주문번호", dto.getCMBN_ORD_NO());
        	hm_1.put("택배사", dto.getHODECO_CODE());
        	hm_1.put("배송상태", dto.getTRCE_STAT_CODE());
        	hm_1.put("현재상태", dto.getRMK4());
        	hm_1.put("출발/도착", dto.getTRCE_STAT_DIV_CODE());
        	hm_1.put("택배사처리일시", dto.getTRCE_STAT_PROC_DT().toString());
        	excellDataList.add(hm_1);
		}
        
        try{
            // 파일 생성
            workbook = Workbook.createWorkbook(file);
            // 시트 생성
            workbook.createSheet("sheet1", 0);
            sheet = workbook.getSheet(0);
            // 셀에 쓰기
            label = new Label(0, 0, "수신일자(롯데기준)");
            sheet.addCell(label);
            label = new Label(1, 0, "운송장번호");
            sheet.addCell(label);
            label = new Label(2, 0, "주문번호");
            sheet.addCell(label);
            label = new Label(3, 0, "택배사");
            sheet.addCell(label);
            label = new Label(4, 0, "배송상태");
            sheet.addCell(label);
            label = new Label(5, 0, "현재상태");
            sheet.addCell(label);
            label = new Label(6, 0, "출발/도착");
            sheet.addCell(label);
            label = new Label(7, 0, "택배사처리일시");
            sheet.addCell(label);

            // 데이터 삽입
            for(int i=0; i < excellDataList.size(); i++){
                HashMap rs = (HashMap)excellDataList.get(i) ;
                label = new Label(0, (i+1), (String)rs.get("수신일자(롯데기준)"));
                sheet.addCell(label);
                label = new Label(1, (i+1), (String)rs.get("운송장번호"));
                sheet.addCell(label);
                label = new Label(2, (i+1), (String)rs.get("주문번호"));
                sheet.addCell(label);
                label = new Label(3, (i+1), (String)rs.get("택배사"));
                sheet.addCell(label);
                label = new Label(4, (i+1), (String)rs.get("배송상태"));
                sheet.addCell(label);
                label = new Label(5, (i+1), (String)rs.get("현재상태"));
                sheet.addCell(label);
                label = new Label(6, (i+1), (String)rs.get("출발/도착"));
                sheet.addCell(label);
                label = new Label(7, (i+1), (String)rs.get("택배사처리일시"));
                sheet.addCell(label);
            }
            // 엑셀에 쓰기 
            workbook.write();
            // 엑셀에 쓰고나서 닫기
            workbook.close();
 
        }catch(Exception e){
            e.printStackTrace();
        }
	}
}
